package com.systop.payment.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.systop.common.exception.BizCodeEnum;
import com.systop.common.utils.PageCheck;
import com.systop.common.validator.group.AddGroup;
import com.systop.common.validator.group.UpdateGroup;
import com.systop.payment.entity.CountPayTypeEntity;
import com.systop.payment.entity.OrderSubjoinEntity;
import com.systop.payment.entity.PaytypeEntity;
import com.systop.payment.service.OrderSubjoinService;
import com.systop.payment.service.PaytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.systop.payment.entity.OrderEntity;
import com.systop.payment.service.OrderService;
import com.systop.common.utils.PageUtils;
import com.systop.common.utils.R;



/**
 * 
 *
 * @author jsj
 * @email jsj@gmail.com
 * @date 2022-05-05 11:46:06
 */
@RestController
@RequestMapping("payment/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private PaytypeService paytypeService;

    @Autowired
    private OrderSubjoinService orderSubjoinService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("payment:order:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询所有订单主表数据
        PageUtils page = orderService.queryPage(params);

        Map<String, Object> resultMap = PageCheck.pageCheck(params, page);

        if(!resultMap.get("page").equals(page.getCurrPage())){
            //当前页与上次访问页不同
            page = orderService.queryPage(params);
        }

        //检测是否为非法查询
        if(page.getCurrPage()>1&&page.getList().size()==0){
            return R.error(BizCodeEnum.INVALID_QUERY.getCode(),BizCodeEnum.INVALID_QUERY.getMsg());
        }

        //查询所有订单从表数据
        List<OrderSubjoinEntity> subList = orderSubjoinService.list();

        page = orderService.findMainData(page,subList);

        return R.ok().put("page", page);
    }

    @RequestMapping("/count/payType")
    public R countPayType(){
        List<PaytypeEntity> payNameList = paytypeService.list(new QueryWrapper<PaytypeEntity>().select("pay_name"));
        List result = new ArrayList();
        for (PaytypeEntity paytypeEntity : payNameList) {
            CountPayTypeEntity countNums = new CountPayTypeEntity();
            long nums = orderService.count(new QueryWrapper<OrderEntity>().eq("paytype",paytypeEntity.getPayName()));
            countNums.setPayName(paytypeEntity.getPayName());
            countNums.setNums(nums);
            result.add(countNums);
        }
        return R.ok().put("countPayType", result);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("payment:order:info")
    public R info(@PathVariable("id") Integer id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("payment:order:save")
    public R save(@Validated({AddGroup.class}) @RequestBody OrderEntity order) throws Exception {
        System.out.println(order);
        orderService.saveOrder(order);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("payment:order:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody OrderEntity order){
		orderService.updateById(order);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("payment:order:delete")
    public R delete(@RequestBody Integer[] ids){
		orderService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
